#include<iostream>
#include<vector>
#include<sstream>
#include<cstring>
using namespace std;
struct treenode {
    treenode* left;
    treenode* right;
    int data = -1;
    int maxn=-0xfffffff;
    treenode * pre=NULL;
    int num;
};
string t;
queue<treenode *>q;
treenode * bfs(treenode *root){
    root->maxn=root->data;
    root->num=1;
    int maxndata=-0x3f3f3f3f;
    int minin=0xffffffff;
    treenode * n;
    q.push(root);
    while(!q.empty()){
        treenode* f=q.front();
        q.pop();
        if(maxndata<f->maxn){
            maxndata=f->maxn;
            n=f;
        }
         else if(maxndata==f->maxn&&minin>f->num){
            minin=f->num;
            n=f;
        }
        if(f->left){
         if(f->maxn>0){
            f->left->maxn=f->maxn+f->left->data;
            f->left->pre=f;
            f->left->num=f->num+1;
         }
         else{
            f->left->maxn=f->left->data;
            f->left->num=1;
         }
         q.push(f->left);
        }
        if(f->right){
         if(f->maxn>0){
            f->right->maxn=f->maxn+f->right->data;
            f->right->pre=f;
            f->right->num=f->num+1;
         }
          else{
            f->right->maxn=f->right->data;
            f->right->num=1;
         }
         q.push(f->right);
        }
    }
     return n;
}
treenode* buildtree(vector<int>& a, int& index) {
    if (index >= a.size()) return nullptr;
    // 如果当前值为0，表示空节点
    if (a[index] == 0) {
        index++;
        return nullptr;
    }
    // 创建当前节点
    treenode* root = new treenode();
    root->data = a[index];
    index++;
    // 递归构建左子树
    root->left = buildtree(a, index);
    // 递归构建右子树
    root->right = buildtree(a, index);
    return root;
}
vector<int>ans;
int mans;
int minl = 0xffffffff;
void dfs(treenode* node, vector<int>&path, int currentnum) {
    if (!node)return;
    currentnum += node->data;
    path.push_back(node->data);
    if (currentnum > mans||currentnum==mans&&minl>path.size()) {
        ans = path;
        minl = path.size();
        mans = currentnum;
    }
    if (node->left) {
        dfs(node->left, path, currentnum);
    }
    if (node->right) {
        dfs(node->right, path, currentnum);
    }
    path.pop_back();
}
void search(treenode* root) {
    if (!root)return;
    vector<int>path;
    dfs(root, path, 0);
    search(root->left);
    search(root->right);
}//每个点都可以是起点。
int main() {
    getline(cin, t);
    stringstream ss(t);
    int a;
    vector<int>b;
    while (ss >> a) {
        b.push_back(a);
    }//去除空格
    int x = 0;
treenode * root = buildtree(b, x);
   treenode * d=bfs(root);
   cout<<d->maxn;
int ans[20002];
int sum=d->num;
for(int i=1;i<=sum;i++){
    ans[sum-i+1]=d->data;
    d=d->pre;
}
cout<<endl;
for(int i=1;i<=sum;i++)cout<<ans[i]<<" ";
return 0;
}
